package demo;

public class Demo14 {
    public static void main(String[] args) {
        /*
         * 将一个整数转换为 7 进制(不要使用API)
         */
        int n = -66;
        String s = Integer.toString(n, 16);
        System.out.println(s);
        s = toString(-66, 16);
        System.out.println(s);
    }
    static char[] digi={'0','1','2','3','4',
            '5','6','7','8','9','a','b',
            'c','d','e','f','g','h','i'};
    public static String toString(int num, int radix){
        char[] chs = new char[32];
        //chs = [0 0 0 0 0 ... 0 - 1 2 3 ]
        //index                ^
        boolean negative = false;
        if (num<0){
            num = -num;
            negative = true;
        }
        int index = chs.length-1;
        do {
            int n = num % radix; //3
            char c = digi[n];//3->'3' 2->'2' ...
            chs[index--] = c;
            //System.out.println(n);
            //System.out.println(num);
            num /= radix;
        }while (num != 0);
        if (negative){
            chs[index--] = '-';
        }
        return new String(chs, index+1,chs.length-index-1);
    }
}
